package org.tbjd.client.dndControllers.projectSwitcher;

import java.util.HashMap;
import java.util.Map;

import org.tbjd.client.constants.NotificationsConstants;
import org.tbjd.client.exceptions.TBJDException;
import org.tbjd.client.interfaces.Decisionmaker;
import org.tbjd.client.interfaces.HasMediator;
import org.tbjd.client.mediators.AbstractMediator;

import com.allen_sauer.gwt.dnd.client.DragContext;
import com.allen_sauer.gwt.dnd.client.drop.SimpleDropController;
import com.google.gwt.user.client.ui.ComplexPanel;
import com.google.gwt.user.client.ui.Widget;

public class StatusDropController extends SimpleDropController implements Decisionmaker, HasMediator {

	private AbstractMediator mediator;

	public StatusDropController(Widget dropTarget) {
		super(dropTarget);
	}

	public void onEnter(DragContext context) {
		super.onEnter(context);
		ComplexPanel dropPanel = (ComplexPanel) context.dropController.getDropTarget();
		Widget toggleButton = dropPanel.getWidget(0);
		toggleButton.addStyleName("statusWidget_toggleButton_engage");

	}

	public void onLeave(DragContext context) {
		super.onLeave(context);
		ComplexPanel dropPanel = (ComplexPanel) context.dropController.getDropTarget();
		Widget toggleButton = dropPanel.getWidget(0);
		toggleButton.removeStyleName("statusWidget_toggleButton_engage");
	}

	public void onDrop(DragContext context) {
		decide(context);
	}

	public void decide(Object action) {
		delegate(action);
	}

	public void delegate(Object action) {
		DragContext context = (DragContext) action;
		Map<Integer, Object> map = new HashMap<Integer, Object>();
		map.put(NotificationsConstants.DROPED_PROJECTS, context.selectedWidgets);
		try {
			this.mediator.decide(map);
		} catch (TBJDException e) {
			e.printStackTrace();
		}
	}

	public void handle(Object action) {
	}

	public void setMediator(AbstractMediator mediator) {
		this.mediator = mediator;
	}

}
